using System;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;

namespace Game.UI
{
    /**
        提示对话框
        只能确定，不能取消
    */
    public class AlertDialog : Dialog
    {
        [SerializeField]
        private TextMeshProUGUI titleText;

        [SerializeField]
        private TextMeshProUGUI messageText;

        // 按钮的文本
        [SerializeField]
        private TextMeshProUGUI confirmButtonText;

        [SerializeField]
        private Animator animator;

        public string Message
        {
            get => messageText.text;
            set => messageText.text = value;
        }

        public string Title
        {
            get => titleText.text;
            set => titleText.text = value;
        }

        public string ConfirmButtonText
        {
            get => confirmButtonText.text;
            set => confirmButtonText.text = value;
        }

        // 事件
        public event Action OnConfirm;

        public void OnConfirmButtonClick()
        {
            // 播放关闭动画
            animator.SetTrigger("Close");

            // 获取动画时长
            float duration = animator.GetCurrentAnimatorStateInfo(0).length;

            // 延迟动画时长后关闭对话框
            Invoke("CloseDialog", duration);
        }

        private void CloseDialog()
        {
            UIManager.Instance.CloseDialog(this);
            OnConfirm?.Invoke();
        }

        public static void Show(string title, string message, Action onConfirm = null)
        {
            UIManager.Instance.ShowDialogAsync("AlertDialog", (dialog) =>
            {
                var alertDialog = dialog as AlertDialog;
                alertDialog.Title = title;
                alertDialog.Message = message;
                alertDialog.OnConfirm = onConfirm;
            });
        }

        public static void Show(string title, string message, string confirmButtonText, Action onConfirm = null)
        {
            UIManager.Instance.ShowDialogAsync("AlertDialog", (dialog) =>
            {
                var alertDialog = dialog as AlertDialog;
                alertDialog.Title = title;
                alertDialog.Message = message;
                alertDialog.ConfirmButtonText = confirmButtonText;
                alertDialog.OnConfirm = onConfirm;
            });
        }
    }
}